package com.jinxin.platform.cdelecinspect.service.impl;

import com.jinxin.platform.cdelecinspect.mapper.*;
import com.jinxin.platform.cdelecinspect.mapper.pojo.*;
import com.jinxin.platform.cdelecinspect.service.EleinspectTaskService;
import com.jinxin.platform.cdelecinspect.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-04-06
 * @Author: Lingnan
 **/
@Service
public class EleinspectTaskServiceImpl implements EleinspectTaskService {
    @Autowired
    private EleinspectTaskMapper taskMapper;
    @Autowired
    private EleinspectRecordMapper recordMapper;
    @Autowired
    private EleinspectMappingPointLineMapper pointLineMapper;
    @Autowired
    private EleinspectMappingPointItemsMapper pointItemsMapper;
    @Autowired
    private EleinspectItemRecordMapper itemRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBatch(List<EleinspectTask> taskList) {
        List<EleinspectTask> list = new ArrayList<>();
        for (EleinspectTask task : taskList) {
            task.setId(Utils.getUuid());
            task.setCreateTime(new Date());
            list.add(task);
        }
        for(EleinspectTask task : list){
            initRecordData(task, task.getId());
        }
        return taskMapper.insertBatch(list);
    }

    @Override
    public int updateBatch(List<EleinspectTask> list) {
        return taskMapper.updateBatch(list);
    }


    @Override
    public List<Map<String,Object>> listTasksPage(HashMap param) {
        return taskMapper.listTasksPage(param);
    }

    @Override
    public int listTasksPageCount(HashMap param) {
        return taskMapper.listTasksPageCount(param);
    }

    @Override
    @Transactional
    public int updateByKey(EleinspectTask task) {
        // 删除ELEINSPECT_ITEM_RECORD表和ELEINSPECT_RECORD表中的记录
        if(task != null && !StringUtils.isEmpty(task.getId())){
            String taskId = task.getId();
            HashMap param = new HashMap();
            param.put("recordTaskId", taskId);
            List<EleinspectRecord> list = recordMapper.list(param);
            List<String> idList = new ArrayList<>();
            for (EleinspectRecord record: list) {
                idList.add(record.getRecordId());
            }
            if(idList.size() > 0){
                // 删除项目记录表数据
                taskMapper.deleteItemsByRecordIds(idList);
            }
            // 删除记录表数据
            taskMapper.deleteRecordByTaskId(taskId);
            initRecordData(task, taskId);
        }
        return taskMapper.updateByKey(task);
    }

    @Override
    public List<EleinspectTask> list(HashMap param) {
        return taskMapper.list(param);
    }

    @Override
    public EleinspectTask selectByKey(String id) {
        return taskMapper.selectByKey(id);
    }

    @Override
    public EleinspectTask selectOne(HashMap param) {
        return taskMapper.selectOne(param);
    }

    @Override
    public int closeTaskWhenOutOfTime() {
        return taskMapper.closeTaskWhenOutOfTime();
    }

    private void initRecordData(EleinspectTask task, String taskId) {
        // 两个记录表初始化相关数据
        HashMap param2 = new HashMap();
        param2.put("mappingLineId", task.getLineId());
        List<EleinspectMappingPointLine> linePointList = pointLineMapper.list(param2);
        for (EleinspectMappingPointLine entity : linePointList) {
            String uid = Utils.getUuid();
            EleinspectRecord record = new EleinspectRecord();
            record.setRecordId(uid);
            record.setRecordLineId(task.getLineId());
            record.setRecordInspectId(task.getStaffId());
            record.setRecordInspectPerson(task.getStaff());
            record.setRecordPointId(entity.getMappingPointId());
            record.setRecordTaskId(taskId);
            int num = recordMapper.insert(record);
            if(num > 0){
                HashMap param3 = new HashMap();
                param3.put("mappingPointId", entity.getMappingPointId());
                List<EleinspectMappingPointItems> itemsList = pointItemsMapper.list(param3);
                for (EleinspectMappingPointItems item: itemsList) {
                    EleinspectItemRecord itemRecord = new EleinspectItemRecord();
                    itemRecord.setItemRecordId(Utils.getUuid());
                    itemRecord.setItemPointRecordId(uid);
                    itemRecord.setItemRecordItemId(item.getMappingItemsId());
                    itemRecordMapper.insert(itemRecord);
                }
            }
        }
    }

}
